fix(gemini): add conversation history truncation to prevent O(N²) token cost growth#1368
Open
vnz wants to merge 1 commit intothedotmack:mainfrom
Open
fix(gemini): add conversation history truncation to prevent O(N²) token cost growth#1368vnz wants to merge 1 commit intothedotmack:mainfrom
vnz wants to merge 1 commit intothedotmack:mainfrom
Conversation
6f2607f to
81553ac
Compare
…en cost growth GeminiAgent sends the full conversation history with every API call, causing quadratic token growth per session. A 100-observation session sends ~30M cumulative input tokens. This ports the proven truncateHistory() sliding window from OpenRouterAgent to GeminiAgent. - Add CLAUDE_MEM_GEMINI_MAX_CONTEXT_MESSAGES (default: 20) and CLAUDE_MEM_GEMINI_MAX_TOKENS (default: 100000) settings - Add truncateHistory() to GeminiAgent using shared estimateTokens() - Always preserve at least the newest message to avoid empty API requests - Add settings validation in SettingsRoutes (1-100 messages, 1K-1M tokens) - Add regression tests for truncation and oversized single-prompt edge case Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
81553ac to
0f84f11
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
truncateHistory()sliding window from OpenRouterAgent to GeminiAgentCLAUDE_MEM_GEMINI_MAX_CONTEXT_MESSAGES(default: 20) andCLAUDE_MEM_GEMINI_MAX_TOKENS(default: 100000) settingscontents: []API requests (a bug also present in OpenRouterAgent)estimateTokens()fromsrc/shared/timeline-formatting.tsinstead of duplicatingFiles Changed
src/shared/SettingsDefaultsManager.tssrc/services/worker/GeminiAgent.tsUSER_SETTINGS_PATHimportsrc/services/worker/http/routes/SettingsRoutes.tstests/gemini_agent.test.tsTest plan
bun test tests/gemini_agent.test.ts— 10/10 tests passbuild-and-sync, worker logs showContext window truncated to prevent runaway costs {originalMessages=22, keptMessages=20}Note
OpenRouterAgent has the same empty-history bug (no
truncated.length > 0guard). Follow-up issue to be opened after this merges.🤖 Generated with Claude Code